package cn.edu.hizee.gps.controller;

import cn.edu.hizee.gps.dao.ContactRepository;
import cn.edu.hizee.gps.dao.UserRepository;
import cn.edu.hizee.gps.dao.UserContactRepository;
import cn.edu.hizee.gps.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZX
 */
@RequestMapping("contact")
@RestController
public class ContactController extends BaseController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ContactRepository contactRepository;

    @Autowired
    private UserContactRepository userContactRepository;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 联系人登陆
     * @param phone
     * @param password
     * @param response
     * @return
     */
    @PostMapping(value = "login_contact")
    public String loginContact(String phone, String password, HttpServletResponse response){
        Contact contact = contactRepository.findAllByContactPhoneAndContactPassword(phone, password);
        if(contact != null){
            return ajaxReturn(response,1,"success",null);
        }
        return ajaxReturn(response,0,"false",null);
    }

    /**
     * 添加联系人
     * @param userId
     * @param response
     * @return
     */
    @PostMapping(value = "add_contact")
    public String addContact(String userId,HttpServletResponse response) {
        Integer id = Integer.valueOf(userId);
        List<User> list = userRepository.findAllById(id);
        if(list.size() == 0){
            return ajaxReturn(response,0,"没有此联系人",null);
        }else {
            for(User user:list) {
                Contact contact = new Contact(user.getId(),user.getRealName(),user.getPhone(),user.getPassword());
                contactRepository.save(contact);
            }
            return  ajaxReturn(response,1,"添加成功",null);
        }

    }

    /**
     * 启用或删除联系人
     * @param response
     * @param userId
     * @param contactIds
     * @param isDisable
     * @return
     */
    @PostMapping("del_contact")
    public String delContact(HttpServletResponse response, Integer userId, List<Integer> contactIds, Integer isDisable){
        for(Integer contactId : contactIds) {
            int flag = userContactRepository.deleteContact(isDisable, userId, contactId);
            if (flag == 1) {
                return ajaxReturn(response, 1, "success", null);
            } else {
                return ajaxReturn(response, 0, "false", null);
            }
        }
        return null;
    }

    /**
     * 传坐标到联系人
     * @param response
     * @param userId
     * @return
     */
    @GetMapping("push_position")
    public String pushSite(HttpServletResponse response, String userId){
        PushInfo pushInfo = (PushInfo) redisTemplate.opsForValue().get(userId + "type1");
        PositionInfo positionInfo = (PositionInfo) redisTemplate.opsForValue().get(userId + "type2");
        if (pushInfo != null) {
            pushInfo.setUserId(userId);
            return ajaxReturn(response, 1, "success", pushInfo, "1");
        }else if(pushInfo == null && positionInfo == null) {
            return ajaxReturn(response, 0, "false", null);
        }else if(positionInfo != null){
            List<String> list = new ArrayList<>();
            list.add(userId);
            positionInfo.setUserId(list);
            return ajaxReturn(response, 1,"success",positionInfo,"2");
        }
        return null;
    }
}
